<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>用户管理</title>
	<#include '/commons/include/head.ftl'>
	<script src="${ctx}/static/vendor/easyui/plugins/datagrid-detailview.js"></script>
</head>

<body>

	<div id="dg">
		<div id="toolbars" style="display:none;">
			<form id="searchForm">
				<div class="datagrid-toolbars">
					<div class="toolbar-row">
						<div class="group">
							<a onclick="form_dialog('add');" href="javascript:;" class="bto bto-green"><i class="fa fa-plus"></i><span>新增</span></a>
							<a onclick="form_dialog('update');" href="javascript:;" class="bto bto-blue"><i class="fa fa-pencil"></i><span>编辑</span></a>
							<a onclick="del();" href="javascript:;" class="bto bto-red"><i class="fa fa-trash"></i><span>删除</span></a>
							<a onclick="reload();" href="javascript:;" class="bto bto-purple1"><i class="fa fa-refresh"></i><span>刷新</span></a>
							<a onclick="locked_dialog();" href="javascript:;" class="bto bto-blue1"><i class="fa fa-gears"></i><span>锁定状态</span></a>
							<a onclick="edit_dialog();" href="javascript:;" class="bto bto-blue1"><i class="fa fa-gears"></i><span>编辑状态</span></a>
							<a onclick="add_role_dialog();" href="javascript:;" class="bto bto-blue1"><i class="fa fa-gears"></i><span>分配角色</span></a>
						</div>
						<div class="group">
							<input name="quickSearch" class="easyui-searchbox" data-options="searcher:quickSearch, width: 150, height: 24,prompt: '快速搜索...'" style="line-height: normal;" />
						</div>
						
						<@shiro.hasRole name="admin">
						<div class="group">
							<a onclick="searchBox();" href="javascript:;" class="bto bto-purple2"><i class="fa fa-search"></i><span>搜索</span></a>
							<a onclick="searchClear();" href="javascript:;" class="bto bto-warning"><i class="fa fa-undo"></i><span>重置</span></a>
						</div>
						</@shiro.hasRole>
						 
					</div>
				</div>
			</form>
		</div>
	</div>
	
	<script>
	var $dg ;
	$(function(){
		$dg = $("#dg").datagrid({
			title: "用户管理",
            url: "${ctx}/manager/system/user/datagrid",
            idField: "id", fit: true, 
            border: false, striped:true, pageSize: 30, pageList: [30,40,50,100,200,500,1000], 
            pagination: true, rownumbers: true, toolbar: "#toolbars", remoteSort: true, 
            sortName: 'sorting', sortOrder: 'asc', queryParams: {},
            frozenColumns: [[
                { field: "ck", checkbox: true },
                { field: "id", title: "ID", width: 80, sortable: true, hidden: true }
            ]],
            columns: [[ 
                { field: "truename", title: "真实姓名", width: 120, formatter:function(value,row){
                	return "<span style='line-height:30px;'>"+value+"</span>" ;
                }},
                { field: "username", title: "登录账号", width: 120 },
                { field: "email", title: "邮箱地址", width: 220 },
                { field: "locked", title: "是否锁定", width: 60, sortable: false, align: "center", formatter:function(value,row){
                	var s1 = "<span style='color: green;'>正常</span>", s2 = "<span style='color: red;'>锁定</span>" ;
                	if(value == -1) { return s2; }
                	if(value == 1) { return s1; }
                }},
                { field: "editable", title: "是否可编辑", width: 75, sortable: false, align: "center", formatter:function(value,row){
                	var s1 = "<span style='color: green;'>正常</span>", s2 = "<span style='color: red;'>不可编辑</span>" ;
                	if(value == -1) { return s2; }
                	if(value == 1) { return s1; }
                }},
                { field: "lastLoginIp", title: "最后登录IP", width: 120 },
                { field: "lastLoginTime", title: "最后登录时间", width: 150, sortable: true, align: "center", formatter:function(value,row){
                	return (undefined != value?$.date.format(value, "yyyy-MM-dd HH:mm:ss"):"") ;	
                }},
                { field: "createTime", title: "创建时间", width: 150, sortable: true, align: "center", formatter:function(value,row){
                	return (undefined != value?$.date.format(value, "yyyy-MM-dd HH:mm:ss"):"") ;	
                }},
                { field: "updateTime", title: "修改时间", width: 150, sortable: true, align: "center", formatter:function(value,row){
                	return (undefined != value?$.date.format(value, "yyyy-MM-dd HH:mm:ss"):"") ;	
                }}
            ]],
            view: detailview,
            detailFormatter:function(index,row){
                return '<div class="ddv" style="padding:5px 0;float:left;"><div id="roles"></div></div>';
            },
            onExpandRow: function(index,row){
                var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                
                var rolesDiv = ddv.children("div#roles");
                rolesDiv.css({"border-right": "1px dashed #9F9F9F", "float": "left"});
                rolesDiv.empty();
                if(undefined != row.auth && row.auth.role_ids.length > 0) {
	                $.post("${ctx}/manager/system/role/selectRoleIn/"+row.auth.role_ids, function(result){
	                	var _dl = $("<dl></dl>");
	                	_dl.css({"margin": "0", "padding": "0"});
	                	
	                	$("<dt style='color:#15428B; font-weight: bold;'>拥有角色</dt>").appendTo(_dl);
	                	$.each(result, function(i, p){
	                		$("<dd></dd>").html(p.name).appendTo(_dl).css({"margin": "0", "padding": "0 10px 0 20px"});;
	                	});
	                	_dl.appendTo(rolesDiv);
	                	
	                	$('#dg').datagrid('fixDetailRowHeight',index);
	                });
	                
                }
                
                $('#dg').datagrid('fixDetailRowHeight',index);
            },
            onLoadSuccess: function(){
            	$.fn.datagrid.extensions.onLoadSuccess.apply(this, arguments);
            	$dg.datagrid('unselectAll');$dg.datagrid('uncheckAll');$dg.datagrid('clearSelections');
            },
            enableHeaderClickMenu: true, enableHeaderContextMenu: true, enableRowContextMenu: false
        });
	});
	function reload(){$dg.datagrid("reload") ;}
	function quickSearch(value,name){
		var o = {} ; o[name] = value ; $dg.datagrid("load",o);
	}
	function searchBox() {
		var search_form = $.util.serializeObject("#searchForm") ;
		if(search_form.quickSearch === "快速搜索...")search_form["quickSearch"]=null;
		$dg.datagrid("load",search_form);
	}
	function searchClear() {
		$("#searchForm").form("clear") ;
		$dg.datagrid("load",{});
	}
	function getIds() {
		var rows = $dg.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			for (var i = 0; i < rows.length; i++) {
				if(undefined != rows[i].id && ""!=rows[i].id) {
					ids.push(rows[i].id);
				}
			}
		}
		return ids ;
	}	
	
	function form_dialog(formType) {
		var form_url = "${ctx}/manager/system/user/form";
		if(formType === "update") {
			var ids = getIds() ;
			if(ids.length == 0) {alertify.warning("请选择一条记录！");return;}
			if(ids.length > 1) {alertify.warning("只能编辑一条记录！");return;}
			
			var rows = $dg.datagrid('getChecked');
			if (rows.length > 0) {
				for (var i = 0; i < rows.length; i++) {
					if(undefined != rows[i].editable && rows[i].editable == -1) {
						alertify.warning("该记录不可编辑");return;
					}
				}
			}
			form_url = "${ctx}/manager/system/user/form?id="+ids[0];
		}
		
		var $d = $.easyui.showDialog({
			title: "<div class='icon-div'><i class='fa fa-link fa-120 pt-2 fa-c-blue'></i></div> 表单", 
			href: form_url, method: "", 
			iniframe: false, topMost: true, iconCls : "",
			width: 610, height: 344, maximizable: true,
            enableApplyButton: false, enableCloseButton: false, enableSaveButton: false,
            buttons : [ 
              { text : "<i class='fa fa-save fa-120 mt-6 fa-c-green'></i> 应用", handler : function() { $.easyui.parent.submitForm($d, $dg, true) ; } },
              { text : "<i class='fa fa-save fa-120 mt-6 fa-c-blue'></i> 保存", handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
              { text : "<i class='fa fa-remove fa-120 mt-6 fa-c-red'></i> 关闭", handler : function() { $d.dialog('destroy'); } } 
           	]
        });
	}
	
	function del() {
		var ids = getIds() ;
		if(ids.length == 0) {alertify.warning("请选择一条记录！");return;}
		$.messager.confirm("您确定要进行该操作？", function (c) { 
			if(c) {
				$.easyui.loading({ msg: "数据删除中，请稍等..."});
				deletedAjax("${ctx}/manager/system/user/delete", {"ids" : ids.join(',')}) ;
			}
		});
	}
	function deletedAjax(url, params) {
		$.post(url, params, function(result) {
			if (result.status) {
				$dg.datagrid("unselectAll");$dg.datagrid("reload") ;
				alertify.success(result.message);
				$.easyui.loaded();
			} else {
				alertify.warning(result.message);
				$.easyui.loaded();
			}
		}, 'json').error(function() { $.easyui.loaded(); });
	}
	
	function locked_dialog() {
		var ids = getIds() ;
		if(ids.length == 0) {alertify.warning("请选择一条记录！");return;}
		
		var $d = $.easyui.showDialog({
			href: "${ctx}/manager/system/user/user_locked", 
			title: "账户状态", iniframe: false, topMost: true, width: 340, height: 147,
            enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
            buttons : [ 
              { text : "<i class='fa fa-save fa-120 mt-6 fa-c-blue'></i> 保存", handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
              { text : "<i class='fa fa-remove fa-120 mt-6 fa-c-red'></i> 关闭", handler : function() { $d.dialog('destroy'); } } 
           	],
           	onLoad: function(){
           		parent.$("#ids").val(ids.join(","));
           	}
        });
	}
	
	function edit_dialog() {
		var ids = getIds() ;
		if(ids.length == 0) {alertify.warning("请选择一条记录！");return;}
		
		var $d = $.easyui.showDialog({
			href: "${ctx}/manager/system/user/user_editable", 
			title: "编辑状态", iniframe: false, topMost: true, width: 340, height: 147,
            enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
            buttons : [ 
              { text : "<i class='fa fa-save fa-120 mt-6 fa-c-blue'></i> 保存", handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
              { text : "<i class='fa fa-remove fa-120 mt-6 fa-c-red'></i> 关闭", handler : function() { $d.dialog('destroy'); } } 
           	],
           	onLoad: function(){
           		parent.$("#ids").val(ids.join(","));
           	}
        });
	}
	
	function add_role_dialog() {
		var ids = getIds() ;
		if(ids.length == 0) {alertify.warning("请选择一条记录！");return;}
		
		var $d = $.easyui.showDialog({
			href: "${ctx}/manager/system/user/user_add_role", 
			title: "添加角色到用户", iniframe: false, topMost: true, width: 740, height: 647,
            enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
            buttons : [ 
              { text : "<i class='fa fa-save fa-120 mt-6 fa-c-blue'></i> 保存", handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
              { text : "<i class='fa fa-remove fa-120 mt-6 fa-c-red'></i> 关闭", handler : function() { $d.dialog('destroy'); } } 
           	],
           	onLoad: function(){
           		parent.$("#ids").val(ids.join(","));
           	}
        });
	}
	
	</script>
</body>
</html>

